﻿using Microsoft.Extensions.Options;
using ModelDto;
using StackExchange.Redis;
using System;
using System.Collections.Generic;
using System.Text;

namespace Common
{
    public class RedisHelper : IRedisHelper
    {
        private readonly IOptions<SystemAppSettings> SysConfig;


        public readonly IConnectionMultiplexer redis;
        public IDatabase db { get; set; }
        public RedisHelper(IOptions<SystemAppSettings> options, IConnectionMultiplexer connectionMultiplexer)
        {
            SysConfig = options;

            redis = connectionMultiplexer;

            int dbIndex = 0;

            int.TryParse(SysConfig.Value.RedisDatabaseIndex, out dbIndex);

            db = redis.GetDatabase(dbIndex);


        }
        /// <summary>
        /// 增加/修改
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public bool SetValue(string key, string value, TimeSpan? expiry = null)
        {
            return db.StringSet(key, value, expiry);
        }

        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public string GetValue(string key)
        {
            return db.StringGet(key);
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public bool DeleteKey(string key)
        {
            return db.KeyDelete(key);
        }

    }
}
